package com.jmp.security.grant.crmPc;

import com.jmp.security.base.ClientDetail;
import com.jmp.security.base.UserDetail;
import com.jmp.security.base.UserDetailService;
import one.stand.mapper.CompanyMapperExt;
import one.stand.model.Company;
import one.stand.model.User;
import one.stand.service.user.UserAdminService;

import java.util.Objects;

public class AdminCompanyUserDetailServiceImpl implements UserDetailService<AdminCompanyRequestDetail> {
    private CompanyMapperExt companyMapperExt;
    private UserAdminService userAdminService;

    public AdminCompanyUserDetailServiceImpl(CompanyMapperExt companyMapperExt, UserAdminService userAdminService) {
        this.companyMapperExt = companyMapperExt;
        this.userAdminService = userAdminService;
    }

    @Override
    public UserDetail get(AdminCompanyRequestDetail requestDetail, ClientDetail clientDetail) {

        UserDetail userDetail = new UserDetail();
        Company companyModel = companyMapperExt.selectByPrimaryKey(requestDetail.getCompanyId());
        userDetail.setCompany(companyModel);
        if (Objects.nonNull(companyModel)) {
            User userModel = userAdminService.getByUserId(companyModel.getUserId());
            userDetail.setUserModel(userModel);
        }
        return userDetail;
    }
}
